﻿using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeFileBrowser();
        }

        private void InitializeFileBrowser()
        {
            string initialPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            LoadTreeView(initialPath);

            treeView1.AfterSelect += TreeView1_AfterSelect;
            listView1.DoubleClick += ListView1_DoubleClick;
        }

        private void LoadTreeView(string path)
        {
            treeView1.Nodes.Clear();
            TreeNode rootNode = new TreeNode(path) { Tag = path };
            treeView1.Nodes.Add(rootNode);
            LoadDirectoryNodes(rootNode);
        }

        private void LoadDirectoryNodes(TreeNode parentNode)
        {
            string path = parentNode.Tag.ToString();
            try
            {
                foreach (string dir in Directory.GetDirectories(path))
                {
                    TreeNode node = new TreeNode(Path.GetFileName(dir)) { Tag = dir };
                    parentNode.Nodes.Add(node);
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            LoadListView(e.Node.Tag.ToString());
        }

        private void LoadListView(string path)
        {
            listView1.Items.Clear();
            try
            {
                foreach (string dir in Directory.GetDirectories(path))
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(dir)) { Tag = dir };
                    item.SubItems.Add("Folder");
                    listView1.Items.Add(item);
                }
                foreach (string file in Directory.GetFiles(path))
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(file)) { Tag = file };
                    item.SubItems.Add("File");
                    listView1.Items.Add(item);
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        private void ListView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string filePath = listView1.SelectedItems[0].Tag.ToString();
                MessageBox.Show($"Double-clicked: {filePath}");
            }
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                LoadListView(treeView1.SelectedNode.Tag.ToString());
            }
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Simple File Browser\nCreated with Windows Forms", "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
